<?php

namespace App\Http\Controllers\Api\Admin;

use App\Exceptions\ConditionException;
use App\Http\Requests\Admin\PermissionRequest;
use App\Http\Resources\Admin\PermissionResource;
use App\Repositories\Admin\PermissionRepository;
use App\Services\PermissionService;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;

/**
 * Class PermissionController
 * @package App\Http\Controllers\Api\Admin\User
 */
class PermissionController extends ApiController
{

    /**
     * @var PermissionRepository
     */
    private $repo;

    /**
     * @var PermissionService
     */
    private $service;

    /**
     * PermissionController constructor.
     * @param PermissionRepository $repo
     * @param PermissionService $service
     */
    public function __construct(PermissionRepository $repo, PermissionService $service)
    {
        $this->repo = $repo;
        $this->service = $service;
        $this->middleware('jwt:admin');
    }

    /**
     * 获取列表信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        $this->hasPermission('permission-main');
        $map = $request->all();
        $list = $this->service->seek($map);
        return $this->success(PermissionResource::collection($list));
    }

    /**
     * 获取详情
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($id)
    {
        $this->hasPermission('permission-main');
        $info = $this->service->find($id);
        return $this->success(new PermissionResource($info));
    }


    /**
     * 创建
     * @param PermissionRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function store(PermissionRequest $request)
    {
        $this->hasPermission('permission-opera');
        $info = $this->service->create($request->all());
        return $this->success(new PermissionResource($info));
    }

    /**
     * 修改
     * @param $id
     * @param PermissionRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update($id, PermissionRequest $request)
    {
        $this->hasPermission('permission-opera');
        $data = $request->all();
        $info = $this->service->find($id);
        $info->fill($data)->save();
        return $this->success(new PermissionResource($info));
    }

    /**
     * 删除记录
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id)
    {
        $this->hasPermission('permission-del');
        throw new ConditionException('请联系技术人员进行权限删改操作！');
        $this->service->delete($id);
        return $this->message('删除成功！');
    }
}
